package com.bxd.baodan.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.bxd.baodan.R;
import com.bxd.baodan.adapter.ListDialogWithIconAdpater;

public class InsureHelperListDialog extends Dialog implements OnItemClickListener {
	private ItemClickListener itemClickListener;
	private int[] iconsResources;
	private String[] titles; 
	private ListView dialog_content;
	private ListDialogWithIconAdpater dialogAdapter;
	private Context context;
	private int textAlign = 0;
	private int kind = 0;

	public InsureHelperListDialog(Context context, int[] iconsResources,
			String[] titles,int textAligin,int kind) {
		super(context);
		this.iconsResources = iconsResources;
		this.titles = titles;
		this.context = context;
		this.textAlign = textAligin;
		this.kind = kind;
	}
	
	public InsureHelperListDialog(Context context, String[] titles, int textAligin,int kind) {
		super(context);
		this.titles = titles;
		this.context = context;
		this.textAlign = textAligin;
		this.kind = kind;
	}
	public ItemClickListener getItemClickListener() {
		return itemClickListener;
	}
	
	public void setItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	private void initData() {
		dialogAdapter = new ListDialogWithIconAdpater(iconsResources, titles,this.getContext(),textAlign);
		dialog_content.setAdapter(dialogAdapter);
		dialog_content.setOnItemClickListener(this);
	}

	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.custom_dialog_list);
		initViews();
		WindowManager m = ((Activity)context).getWindowManager();
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		p.width = (int) (d.getWidth() * 0.9); // 宽度设置为屏幕的0.95
		if(this.kind == 1){
			p.height = (int) (d.getHeight() * 0.5); // 宽度设置为屏幕的0.95
		}
		this.getWindow().setAttributes(p);
		initData();
	}
	
	private void initViews() {
		this.dialog_content = (ListView) this.findViewById(R.id.dialog_content_list);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		itemClickListener.onItemClick(arg2);
		this.cancel();
	}
	public interface ItemClickListener{
		abstract void onItemClick(int position);
	}
}
